<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>轮胎使用记录查询结果</title>
    <link href="https://cdn.bootcdn.net/ajax/libs/bootstrap/5.1.3/css/bootstrap.min.css" rel="stylesheet">
    <style>
        .card {
            box-shadow: 0 0.125rem 0.25rem rgba(0, 0, 0, 0.075);
            margin-bottom: 1.5rem;
        }
        .usage-stats {
            background: linear-gradient(135deg, #6c757d 0%, #495057 100%);
            color: white;
        }
        .record-pre {
            white-space: pre-wrap;
            word-wrap: break-word;
            background-color: #f8f9fa;
            padding: 1rem;
            border-radius: 0.25rem;
            font-family: inherit;
            font-size: 0.875rem;
            line-height: 1.5;
        }
    </style>
</head>
<body>
<div class="container mt-4">
    <h1>轮胎使用记录查询结果</h1>

    <div th:if="${stats != null}">
        <!-- 使用统计卡片 -->
        <div class="row">
            <div class="col-md-12">
                <div class="card usage-stats">
                    <div class="card-body">
                        <div class="row text-center">
                            <div class="col-md-3">
                                <h5>使用公里数</h5>
                                <h2 th:if="${stats.usedMileage != null}" th:text="${stats.usedMileage} + ' 公里'"></h2>
                                <h2 th:if="${stats.usedMileage == null}" class="text-warning">使用中</h2>
                            </div>
                            <div class="col-md-3">
                                <h5>使用时间</h5>
                                <h2 th:if="${stats.usedDays != null}" th:text="${stats.usedDays} + ' 天'"></h2>
                                <h2 th:if="${stats.usedDays == null}" class="text-warning">使用中</h2>
                            </div>
                            <div class="col-md-3">
                                <h5>状态</h5>
                                <h2>
                                    <span th:if="${stats.scrapDate == null}" class="badge bg-success">使用中</span>
                                    <span th:if="${stats.scrapDate != null}" class="badge bg-secondary">已报废</span>
                                </h2>
                            </div>
                            <div class="col-md-3">
                                <h5>总费用</h5>
                                <h2 th:if="${stats.totalCost != null}" th:text="'￥' + ${#numbers.formatDecimal(stats.totalCost, 1, 2)}"></h2>
                                <h2 th:if="${stats.totalCost == null}">--</h2>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <!-- 基本信息 -->
        <div class="card mt-4">
            <div class="card-header">
                <h5 class="mb-0">基本信息</h5>
            </div>
            <div class="card-body">
                <div class="row">
                    <div class="col-md-6">
                        <p><strong>轮胎号:</strong> <span th:text="${stats.tireNumber}"></span></p>
                        <p><strong>当前关联车牌号:</strong> <span th:text="${stats.currentPlateNumber ?: '未安装'}"></span></p>
                        <p><strong>品牌:</strong> <span th:text="${stats.tireBrand ?: '--'}"></span></p>
                        <p><strong>型号:</strong> <span th:text="${stats.tireModel ?: '--'}"></span></p>
                    </div>
                    <div class="col-md-6">
                        <p><strong>装胎日期:</strong> <span th:text="${stats.installDate ?: '--'}"></span></p>
                        <p><strong>装胎公里数:</strong> <span th:text="${stats.installMileage ?: '--'}"></span></p>
                        <p><strong>报废日期:</strong> <span th:text="${stats.scrapDate ?: '--'}"></span></p>
                        <p><strong>报废公里数:</strong> <span th:text="${stats.scrapMileage ?: '--'}"></span></p>
                    </div>
                </div>
            </div>
        </div>

        <!-- 费用信息 -->
        <div class="card">
            <div class="card-header">
                <h5 class="mb-0">费用信息</h5>
            </div>
            <div class="card-body">
                <div class="row">
                    <div class="col-md-3">
                        <p><strong>轮胎单价:</strong>
                            <span th:if="${stats.tirePrice != null}" th:text="'￥' + ${#numbers.formatDecimal(stats.tirePrice, 1, 2)}"></span>
                            <span th:if="${stats.tirePrice == null}">--</span>
                        </p>
                    </div>
                    <div class="col-md-3">
                        <p><strong>装胎费用:</strong>
                            <span th:if="${stats.installCost != null}" th:text="'￥' + ${#numbers.formatDecimal(stats.installCost, 1, 2)}"></span>
                            <span th:if="${stats.installCost == null}">--</span>
                        </p>
                    </div>
                    <div class="col-md-3">
                        <p><strong>修补费用:</strong>
                            <span th:if="${stats.repairCost != null}" th:text="'￥' + ${#numbers.formatDecimal(stats.repairCost, 1, 2)}"></span>
                            <span th:if="${stats.repairCost == null}">--</span>
                        </p>
                    </div>
                    <div class="col-md-3">
                        <p><strong>调胎费用:</strong>
                            <span th:if="${stats.swapCost != null}" th:text="'￥' + ${#numbers.formatDecimal(stats.swapCost, 1, 2)}"></span>
                            <span th:if="${stats.swapCost == null}">--</span>
                        </p>
                    </div>
                </div>
            </div>
        </div>

        <!-- 记录信息 -->
        <div class="row">
            <div class="col-md-6">
                <!-- 修补记录 -->
                <div class="card">
                    <div class="card-header d-flex justify-content-between align-items-center">
                        <h5 class="mb-0">修补记录</h5>
                        <!-- 简化处理，直接显示固定文本 -->
                        <span class="badge bg-warning" th:text="'记录详情'"></span>
                    </div>
                    <div class="card-body">
                        <div th:if="${stats.repairRecords != null and !stats.repairRecords.isEmpty()}">
                            <pre class="record-pre" th:text="${stats.repairRecords}"></pre>
                        </div>
                        <div th:if="${stats.repairRecords == null or stats.repairRecords.isEmpty()}" class="text-center text-muted">
                            <p>暂无修补记录</p>
                        </div>
                    </div>
                </div>
            </div>
            <div class="col-md-6">
                <!-- 调换记录 -->
                <div class="card">
                    <div class="card-header d-flex justify-content-between align-items-center">
                        <h5 class="mb-0">调换记录</h5>
                        <!-- 简化处理，直接显示固定文本 -->
                        <span class="badge bg-info" th:text="'记录详情'"></span>
                    </div>
                    <div class="card-body">
                        <div th:if="${stats.swapRecords != null and !stats.swapRecords.isEmpty()}">
                            <pre class="record-pre" th:text="${stats.swapRecords}"></pre>
                        </div>
                        <div th:if="${stats.swapRecords == null or stats.swapRecords.isEmpty()}" class="text-center text-muted">
                            <p>暂无调换记录</p>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <div th:if="${stats == null}" class="alert alert-warning mt-4">
        <h4>未找到轮胎记录</h4>
        <p>请检查轮胎号是否正确，或该轮胎尚未录入系统。</p>
    </div>

    <div class="mt-4">
        <a href="/api/tires/usage" class="btn btn-primary">继续查询</a>
        <a href="/api/tires" class="btn btn-secondary">返回轮胎列表</a>
    </div>
</div>

<script>
    // 使用JavaScript在客户端计算记录条数
    document.addEventListener('DOMContentLoaded', function() {
        // 计算修补记录条数
        const repairPre = document.querySelector('pre[th\\:text="${stats.repairRecords}"]');
        if (repairPre) {
            const repairText = repairPre.textContent;
            if (repairText && repairText.trim() !== '') {
                const repairLines = repairText.split('\n').filter(line => line.trim() !== '');
                const repairBadge = document.querySelector('.card:has(.card-header h5:contains("修补记录")) .badge.bg-warning');
                if (repairBadge) {
                    repairBadge.textContent = repairLines.length + ' 条记录';
                }
            }
        }

        // 计算调换记录条数
        const swapPre = document.querySelector('pre[th\\:text="${stats.swapRecords}"]');
        if (swapPre) {
            const swapText = swapPre.textContent;
            if (swapText && swapText.trim() !== '') {
                const swapLines = swapText.split('\n').filter(line => line.trim() !== '');
                const swapBadge = document.querySelector('.card:has(.card-header h5:contains("调换记录")) .badge.bg-info');
                if (swapBadge) {
                    swapBadge.textContent = swapLines.length + ' 条记录';
                }
            }
        }
    });
</script>
</body>
</html>